// 粒子效果模块
function initParticlesEffect(canvas) {
    // 获取2D渲染上下文
    const ctx = canvas.getContext('2d');
    const particles = [];
    const particleCount = 100;
    
    // 创建粒子
    for (let i = 0; i < particleCount; i++) {
        particles.push({
            x: Math.random() * canvas.width,
            y: Math.random() * canvas.height,
            size: Math.random() * 3 + 1,
            speedX: (Math.random() - 0.5) * 1.5,
            speedY: (Math.random() - 0.5) * 1.5,
            color: `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.random() * 0.8 + 0.2})`,
            connections: []
        });
    }
    
    // 设置多彩渐变背景（替代单调的黑色）
    function drawBackground() {
        const gradient = ctx.createRadialGradient(
            canvas.width / 2, canvas.height / 2, 0,
            canvas.width / 2, canvas.height / 2, Math.max(canvas.width, canvas.height)
        );
        
        // 随时间变化的多彩渐变
        const timeFactor = Date.now() * 0.0001;
        const hue1 = 220 + Math.sin(timeFactor) * 40;
        const hue2 = (hue1 + 80) % 360;
        
        gradient.addColorStop(0, `hsl(${hue1}, 70%, 15%)`);
        gradient.addColorStop(0.5, `hsl(${(hue1 + hue2) / 2}, 65%, 10%)`);
        gradient.addColorStop(1, `hsl(${hue2}, 60%, 5%)`);
        
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
    }
    
    // 计算粒子之间的连接
    function drawConnections() {
        for (let i = 0; i < particles.length; i++) {
            for (let j = i + 1; j < particles.length; j++) {
                const dx = particles[i].x - particles[j].x;
                const dy = particles[i].y - particles[j].y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                
                // 如果粒子距离较近，绘制连接线
                if (distance < 150) {
                    const opacity = 1 - (distance / 150);
                    ctx.beginPath();
                    ctx.strokeStyle = `rgba(255, 255, 255, ${opacity * 0.3})`;
                    ctx.lineWidth = 0.5;
                    ctx.moveTo(particles[i].x, particles[i].y);
                    ctx.lineTo(particles[j].x, particles[j].y);
                    ctx.stroke();
                }
            }
        }
    }
    
    // 动画循环
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        // 绘制背景
        drawBackground();
        
        // 绘制连接线
        drawConnections();
        
        // 更新和绘制粒子
        particles.forEach(particle => {
            // 移动粒子
            particle.x += particle.speedX;
            particle.y += particle.speedY;
            
            // 边界检测，使粒子反弹
            if (particle.x < 0 || particle.x > canvas.width) particle.speedX *= -1;
            if (particle.y < 0 || particle.y > canvas.height) particle.speedY *= -1;
            
            // 绘制粒子
            ctx.beginPath();
            ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
            ctx.fillStyle = particle.color;
            ctx.fill();
        });
        
        requestAnimationFrame(animate);
    }
    
    animate();
}

// 暴露函数到全局作用域
window.initParticlesEffect = initParticlesEffect;